package com.atwisdom.star.core.dao.pojo.modelClass.hier;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import jodd.util.StringUtil;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * @TableName hier_model
 */
@TableName(value ="hier_model",autoResultMap = true)
@Data
public class HierModel implements Serializable {
    /**
     * 唯一标识
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 容器类型
     */
    @TableField(value = "type")
    private String type;

    /**
     * model名称
     */
    @TableField(value = "model")
    private String model;

    /**
     * 节点名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 容器类名
     */
    @TableField(value = "className")
    private String className;

    /**
     * 树结构，上级或父节点id
     */
    @TableField(value = "parentId")
    private String parentId;

    /**
     * 前驱节点
     */
    @TableField(value = "prev", typeHandler = FastjsonTypeHandler.class)
    protected JSONArray prev;

    /**
     * 节点id
     */
    @TableField(value = "nodeId")
    private String nodeId;

    /**
     * 节点类型，如root，node，leaf
     */
    @TableField(value = "nodeType")
    private String nodeType;

    /**
     * 属性信息
     */
    @TableField(value = "properties", typeHandler = FastjsonTypeHandler.class)
    protected JSONArray properties;

    /**
     * 属性信息
     */
    @TableField(value = "statusProperties", typeHandler = FastjsonTypeHandler.class)
    protected JSONArray statusProperties;

    /**
     * 连接id
     */
    @TableField(value = "linkId")
    private Long linkId;

    /**
     * 连接表名
     */
    @TableField(value = "linkModel")
    private String linkModel;

    /**
     * 连接class
     */
    @TableField(value = "linkClassName")
    private String linkClassName;

    /**
     * 连接类型
     */
    @TableField(value = "linkType")
    private String linkType;

    /**
     * 备注
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 创建人账号
     */
    @TableField(value = "createdBy",fill = FieldFill.INSERT)
    protected String createdBy;

    /**
     * 创建时间
     */
    @TableField(value = "createTime",fill = FieldFill.INSERT)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime createTime;

    /**
     * 修改人账号
     */
    @TableField(value = "updatedBy",fill = FieldFill.INSERT_UPDATE)
    protected String updatedBy;

    /**
     * 修改时间
     */
    @TableField(value = "updateTime",fill = FieldFill.INSERT_UPDATE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime updateTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    /**
     * 获取非果询属values
     * @return 返回结果
     */
    public JSONObject fetchNonFeatureValues() {
        JSONObject values = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                values.put(itemJson.getString("name"), itemJson.get("defaultValue"));
            }
        }
        return values;
    }

    public JSONObject fetchNonFeatureTypeProperties() {
        JSONObject resultJson = new JSONObject();
        for (Object item : properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                resultJson.put(itemJson.getString("name"), itemJson.getString("type"));
            }
        }
        return resultJson;
    }


    /**
     * 获取非查询默认values
     * @return 返回结果
     */
    public JSONObject fetchNonFeatures() {
        JSONObject nonFeatures = new JSONObject();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                nonFeatures.put(itemJson.getString("name"), itemJson.get("defaultValue"));
            }
        }
        return nonFeatures;
    }
    /**
     * 获取查询属性 featureProperties
     * @return 返回结果
     */
    public List<JSONObject> fetchFeatureProperties() {
        List<JSONObject> featureProperties = new ArrayList<>();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isNotBlank(itemJson.getString("code"))) {
                featureProperties.add(itemJson);
            }
        }
        Comparator<JSONObject> compByLength = (j1, j2) -> j2.getString("name").length() - j1.getString("name").length();

        return featureProperties.stream().sorted(compByLength).collect(Collectors.toList());
    }
    /**
     * 获取非查询属性 nonFeaturesProperties;
     * @return 返回结果
     */
    public List<JSONObject> fetchNonFeaturesProperties() {
        List<JSONObject> nonFeaturesProperties = new ArrayList<>();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isBlank(itemJson.getString("code"))) {
                nonFeaturesProperties.add(itemJson);
            }
        }
        return nonFeaturesProperties;
    }

    /**
     * 获取查询属性 featureProperties
     * @return Map<String, JSONObject>   k 字段名称， value 整个属性
     */
    public Map<String, JSONObject> fetchFeatureMap() {
        Map<String, JSONObject> featureMap = new HashMap<>();
        List<JSONObject> featureProperties = new ArrayList<>();
        for (Object item : this.properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            if (StringUtil.isNotBlank(itemJson.getString("code"))) {
                featureProperties.add(itemJson);
                featureMap.put(itemJson.getString("name"), itemJson);
            }
        }
        return featureMap;
    }

}